using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;

namespace PointBridge.SPDX
{
    public class SPDXConfig
    {
        #region Constructors
        public SPDXConfig(string p_title, string p_contentTypesXml, string p_listsXml, string p_myNamespace, string p_outputDir, bool p_isDeriveFromMyClass, string p_deriveFromClassName)
        {
            Initialize(p_title, p_contentTypesXml, p_listsXml, p_myNamespace, p_outputDir, p_isDeriveFromMyClass, p_deriveFromClassName);
        }
        public SPDXConfig(string p_contentTypesXml, string p_listsXml, string p_myNameSpace, string p_outputDir)
        {
            Initialize("", p_contentTypesXml, p_listsXml, p_myNameSpace, p_outputDir, false, "");
        }
        public SPDXConfig(SPListItem item)
        {
            string p_title;
            string p_contentTypesXml;
            string p_listsXml;
            string p_myNamespace;
            string p_outputDir;
            bool p_isDeriveFromMyClass;
            string p_deriveFromClassName;

            if (item != null)
            {
                try
                {
                    if (item["Title"] != null) p_title = (string)item["Title"];
                    else p_title = "";

                    if (item["ContentTypesXML"] != null) p_contentTypesXml = (string)item["ContentTypesXML"];
                    else p_contentTypesXml = "";

                    if (item["ListsXML"] != null) p_listsXml = (string)item["ListsXML"];
                    else p_listsXml = "";

                    if (item["Namespace"] != null) p_myNamespace = (string)item["Namespace"];
                    else p_myNamespace = "";

                    if (item["OutputDir"] != null) p_outputDir = (string)item["OutputDir"];
                    else p_outputDir = "";

                    if (item["IsDeriveFromMyClass"] != null) p_isDeriveFromMyClass = (bool)item["IsDeriveFromMyClass"];
                    else p_isDeriveFromMyClass = false;

                    if (item["DeriveFromClassName"] != null) p_deriveFromClassName = (string)item["DeriveFromClassName"];
                    else p_deriveFromClassName = "";
                }
                catch (Exception ex)
                {
                    throw new PointBridgeEx("Unable to initilize SPDXConfig from item: " + ex.Message);
                }
            }
            else
            {
                p_title = "";
                p_contentTypesXml = "";
                p_listsXml = "";
                p_myNamespace = "";
                p_outputDir = "";
                p_isDeriveFromMyClass = false;
                p_deriveFromClassName = "";
            }
            Initialize(p_title, p_contentTypesXml, p_listsXml, p_myNamespace, p_outputDir, p_isDeriveFromMyClass, p_deriveFromClassName);
        } 
        #endregion

        #region Methods
        public void Initialize(string p_title, string p_contentTypesXml, string p_listsXml, string p_myNamespace, string p_outputDir, bool p_isDeriveFromMyClass, string p_deriveFromClassName)
        {
            title = p_title;
            contentTypesXml = p_contentTypesXml;
            listsXml = p_listsXml;
            myNamespace = p_myNamespace;
            outputDir = p_outputDir;
            isDeriveFromMyClass = p_isDeriveFromMyClass;
            deriveFromClassName = p_deriveFromClassName;
        }
        public void SaveIntoSPItem(SPItem item)
        {
            item["Title"] = title;
            item["ContentTypesXML"] = contentTypesXml;
            item["ListsXML"] = listsXml;
            item["Namespace"] = myNamespace;
            item["OutputDir"] = outputDir;
            item["IsDeriveFromMyClass"] = isDeriveFromMyClass;
            item["DeriveFromClassName"] = deriveFromClassName;
        }
        public void Validate()
        {
            if (isDeriveFromMyClass == true && deriveFromClassName.Trim() == "") throw new ValidationEx("Must enter class to inherit from.", "wDeriveFromClassName");
            if (isDeriveFromMyClass == false && deriveFromClassName.Trim() != "") throw new ValidationEx("Class to inherit from should be empty.", "wDeriveFromClassName");
            if (myNamespace == "") throw new ValidationEx("Must Enter Namspace.", "wNameSpace");
            if (outputDir == "") throw new ValidationEx("Must Enter Output Directory.", "wOutputDir");
        } 
        #endregion

        #region Public Properties
        public string title;
        public string contentTypesXml;
        public string listsXml;
        public bool isDeriveFromMyClass;
        public string deriveFromClassName;
        public string myNamespace;
        public string outputDir; 
        #endregion
    }
}
